package com.thinkfit.jy.fragment;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.constant.Constants;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.HeartRateRunActivity;
import com.thinkfit.jy.activity.ProceduralTrainingActivity;
import com.thinkfit.jy.activity.SetTargetActivity;
import com.thinkfit.jy.activity.TestActivity;
import com.thinkfit.jy.activity.TrainPatternActivity;
import com.thinkfit.jy.activity.VideoActivity;
import com.thinkfit.jy.utils.ToastUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 运动中心
 */
public class SportsCenterFragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    ConstraintLayout clMain,clTarget,clImmersive,clDrill,clHeart;
    AnimatorSet animatorSetShow,animatorSetHide;

    boolean isShowMenu = true;
    public static SportsCenterFragment getInstance(int index) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.BUNDLE_EXTRA, index);
//        bundle.putInt(Constants.BUNDLE_EXTRA_2, type);
        SportsCenterFragment fragment = new SportsCenterFragment();
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sports_center;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initView(view);
    }

    public void initView(View view){
        clMain = view.findViewById(R.id.cl_main);
        clTarget = view.findViewById(R.id.cl_target);
        clImmersive = view.findViewById(R.id.cl_immersive);
        clDrill = view.findViewById(R.id.cl_drill);
        clHeart = view.findViewById(R.id.cl_heart);

        clTarget.setOnClickListener(this);
        clImmersive.setOnClickListener(this);
        clDrill.setOnClickListener(this);
        clHeart.setOnClickListener(this);
        clTarget.setVisibility(View.INVISIBLE);
        clImmersive.setVisibility(View.INVISIBLE);
        clDrill.setVisibility(View.INVISIBLE);
        clHeart.setVisibility(View.INVISIBLE);

        delay();
    }

    // 弹出菜单
    public void showMenu() {
        int width = clImmersive.getWidth();
        ObjectAnimator firstAnimator = ObjectAnimator.ofFloat(clImmersive,
                "translationX", width, -(width + 10) * 3);
        ObjectAnimator secondAnimator = ObjectAnimator.ofFloat(clTarget,
                "translationX", width, -(width + 10) * 2);
        ObjectAnimator thirdAnimator = ObjectAnimator.ofFloat(clDrill,
                "translationX", width, -(width + 10) * 1);
        ObjectAnimator fourAnimator = ObjectAnimator.ofFloat(clHeart,
                "translationX", width, 0);
        if(animatorSetShow == null){
            animatorSetShow = new AnimatorSet();
            animatorSetShow.setDuration(500);
            animatorSetShow.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(@NonNull Animator animation) {
                    clTarget.setVisibility(View.VISIBLE);
                    clImmersive.setVisibility(View.VISIBLE);
                    clDrill.setVisibility(View.VISIBLE);
                    clHeart.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(@NonNull Animator animation) {
                    isShowMenu = false;
                }

                @Override
                public void onAnimationCancel(@NonNull Animator animation) {

                }

                @Override
                public void onAnimationRepeat(@NonNull Animator animation) {

                }
            });

        }
        animatorSetShow.playTogether(firstAnimator,secondAnimator,thirdAnimator,fourAnimator);
        animatorSetShow.start();
    }
    public void hideMenu() {
        int width = clImmersive.getWidth();
        ObjectAnimator firstAnimator = ObjectAnimator.ofFloat(clImmersive,
                "translationX", -(width + 10) * 3, 0);
        ObjectAnimator secondAnimator = ObjectAnimator.ofFloat(clTarget,
                "translationX", -(width + 10) * 2, 0);
        ObjectAnimator thirdAnimator = ObjectAnimator.ofFloat(clDrill,
                "translationX", -(width + 10) * 1, 0);
        if(animatorSetHide == null){
            animatorSetHide = new AnimatorSet();
            animatorSetHide.setDuration(500);
            animatorSetHide.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(@NonNull Animator animation) {

                }

                @Override
                public void onAnimationEnd(@NonNull Animator animation) {
                    isShowMenu = true;
                }

                @Override
                public void onAnimationCancel(@NonNull Animator animation) {

                }

                @Override
                public void onAnimationRepeat(@NonNull Animator animation) {

                }
            });

        }
        animatorSetHide.playTogether(firstAnimator,secondAnimator,thirdAnimator);
        animatorSetHide.start();
    }
    Disposable delayDisposable;
    public void delay(){
        Observable.timer(300, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        delayDisposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        showMenu();
                        closeDelayDisposable();
                    }

                    @Override
                    public void onError(Throwable e) {
                        closeDelayDisposable();
                    }

                    @Override
                    public void onComplete() {
                        closeDelayDisposable();
                    }
                });
    }

    public void closeDelayDisposable(){
        if(delayDisposable != null){
            delayDisposable.dispose();
            delayDisposable = null;
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   SportsCenterFragment    onResume");

    }


    @Override
    public void onStart() {
        super.onStart();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   SportsCenterFragment    onStart");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   SportsCenterFragment    onStop");
//        DeviceTypeUtils.showBackButton(getContext().getApplicationContext());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   SportsCenterFragment    onDestroyView");
        animatorSetHide = null;
        animatorSetShow = null;
        isShowMenu = true;
    }

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cl_target:
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.BUNDLE_EXTRA, 1);
                openActivity(SetTargetActivity.class,bundle);
                break;
            case R.id.cl_immersive:
                bundle = new Bundle();
                bundle.putInt(Constants.BUNDLE_EXTRA, 4);
                openActivity(VideoActivity.class, bundle);
                break;
            case R.id.cl_drill:
                bundle = new Bundle();
                bundle.putInt(Constants.BUNDLE_EXTRA, 1);
                openActivity(TrainPatternActivity.class, bundle);
                break;
            case R.id.cl_heart:
                ToastUtils.getInstance().show(getContext(),getString(R.string.app_toast_hint_2));
//                bundle = new Bundle();
//                bundle.putInt(Constants.BUNDLE_EXTRA, 1);
//                openActivity(HeartRateRunActivity.class, bundle);
//                openActivity(TestActivity.class);
//                if(isShowMenu){
//                    showMenu();
//                }else {
//                    hideMenu();
//                }

//                ToastUtils.showShort(getString(R.string.app_toast_hint_2));
                break;
        }
    }

}
